<template>
<!-- 新建工艺版本的第一个弹窗 -->
  <el-dialog
    width="400px"
    :close-on-click-modal="false"
    :title="title"
    @close="close"
    :visible.sync="visible"
    append-to-body
    class="six"
  >
    <el-form :model="formData" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="版本名称" prop="versionName">
        <el-input
          v-model="formData.versionName"
          placeholder="输入版本名称"
          class="w150"
        ></el-input>
      </el-form-item>
      <el-form-item label="图号" prop="drawingNumber">
        <el-input
          v-model="formData.drawingNumber"
          placeholder="输入图号"
          class="w150"
          :disabled="isHaveDrawing"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          placeholder="输入备注"
          class="w150"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="footer">
      <el-button type="primary" @click="submitForm">下一步</el-button>
      <el-button @click="close">取消</el-button>
      <dialog-1 ref="dialog1" @update="createdSuccess"></dialog-1>
    </div>
  </el-dialog>
</template>

<script lang="ts">
import { Vue, Component } from "vue-property-decorator";
import { State, namespace } from "vuex-class";
import Dialog1 from "./addProcessDetailDialog.vue";

const vuexProcess = namespace("TwoProcess");
@Component({
  components: {
    Dialog1,
  },
  props: {
    'isHaveDrawing': {
      type: Boolean,
      default: false,
    }
  }
})
export default class CreateProcessDialog extends Vue {
  public $refs!: {
    form: any;
    dialog1: any;
  };

  @vuexProcess.Action CREAT_VERSION: any;
  @vuexProcess.Action PREDATA_VERSION: any;

  visible: boolean = false;
  title: string = "新增工艺版本";
  level: number = 1;
  formData: any = {
    drawingNumber: "",
    versionName: "",
    remark: "",
  };
  rules: any = {
    versionName: [{ required: true, message: "请填写版本名称", trigger: "blur" }],
    drawingNumber: [{ required: true, message: "请填写图号", trigger: "blur" }],
  };

  getData(data) {
    this.formData.drawingNumber = data;
  }

  open(drawingNumber = "") {
    if(drawingNumber) {
      this.formData.drawingNumber = drawingNumber;
    }
    this.visible = true;
  }
  close() {
    this.$refs.form.resetFields();
    this.level = 1;
    this.formData = {
      drawingNumber: "",
      versionName: "",
    };
    this.visible = false;
  }
  submitForm() {
    this.$refs.form.validate((valid: any) => {
      if (valid) {
        this.preCreate(this.formData);
      }
    });
  }

  async preCreate(data: any) {
    let result = await this.CREAT_VERSION({ ...data });
    let getData = await this.PREDATA_VERSION();
    if (result && getData) {
      this.$refs.dialog1.open(true);
      this.$refs.dialog1.getAdd(this.formData.drawingNumber, this.formData.versionName);
      this.visible = false;
      this.$emit('update');// 通知父组件更新工艺版本数据
    }
  }

  createdSuccess() {
    this.$emit('update');// 通知父组件更新工艺版本数据
  }
}
</script>

<style lang="scss" scoped>
.six {
  .footer {
    border-top: 1px solid #eee;
    padding: 10px 20px;
    background: #fefefe;
    text-align: center;
  }
}
</style>
